from appium import webdriver
#WebDriverWait用来加入时间判断，有时候控件元素需要过一段时间才会出现
from selenium.webdriver.support.ui import WebDriverWait
import time
num=1
options={
        "platformName": "android",
          "platformVersion": "5.1.1",
          "deviceName": "emulator-5554",
          "addPackage": "com.android.settings",
          "appActivity": "com.android.settings.Settings",
          "noReset": True,
          "unicodekeyboard": True,
          "resetkeyboard": True
}

driver = webdriver.Remote("http://localhost:4723/wd/hub",options)

#得到窗口大小
def get_size():
    x=driver.get_window_size()['width']
    y=driver.get_window_size()['height']
    return x, y

def getuser():
    global num
    size = get_size()
    time.sleep(1)
    try:#上划
        # 定义滑动
        x1 = int(size[0] * 0.5)
        x2 = int(size[0] * 0.7)
        y1 = int(size[1] * 0.9)
        y2 = int(size[1] * 0.2)
        # 模拟滑动
        time.sleep(1)
        driver.swipe(x1, y1, x2, y2)
        print("**第{0}次上划**".format(num))
    except:
        pass

    time.sleep(1)
    try:#左滑
        # 定义滑动
        x1 = int(size[0] * 0.9)
        x2 = int(size[0] * 0.3)
        y1 = int(size[1] * 0.5)
        y2 = int(size[1] * 0.5)
        # 模拟滑动
        time.sleep(1)
        driver.swipe(x1, y1, x2, y2)
        print("**第{0}次左滑**".format(num))
    except:
        pass

    # 点进去具体界面

    time.sleep(1)
    try:
        if WebDriverWait(driver, 5).until(lambda x: x.find_element_by_id('com.ss.android.ugc.aweme:id/l6')):
            # 进入用户信息界面
            print("**抓取第{0}个用户数据**".format(num))
            time.sleep(1)
            driver.find_element_by_id('com.ss.android.ugc.aweme:id/l6').click()  # 返回
            # driver.find_element_by_id('com.ss.android.ugc.aweme:id/bgg').click()#头像
    except:
        pass


time.sleep(10)#延迟十秒运行，主要是博主这边比较卡
for num in range(1,1000):#设置执行测试
    print("————开始执行第{0}次".format(num))
    getuser()
    print("第{0}次执行完成————".format(num))

